<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  

  
  <title>Base64算法原理 | SmileYi</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="概述Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一，常用于传递HTTP中较长的标识信息、或者对于一些信息进行简单的加密。当我们用记事本打开一些二进制文件时会显示一堆乱码，主要是因为二进制文件中包含了很多无法显示和打印的字符，这种情况下可以对数据进行base64编码，使得记事本等文本工具可以对其处理。 Base64编码原理是将原始数据分组，每组3个字节24位。再将每组的24位拆成">
<meta name="keywords" content="原创">
<meta property="og:type" content="article">
<meta property="og:title" content="Base64算法原理">
<meta property="og:url" content="http://smileyi.gitee.io/2019/04/09/Base64算法原理/index.html">
<meta property="og:site_name" content="SmileYi">
<meta property="og:description" content="概述Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一，常用于传递HTTP中较长的标识信息、或者对于一些信息进行简单的加密。当我们用记事本打开一些二进制文件时会显示一堆乱码，主要是因为二进制文件中包含了很多无法显示和打印的字符，这种情况下可以对数据进行base64编码，使得记事本等文本工具可以对其处理。 Base64编码原理是将原始数据分组，每组3个字节24位。再将每组的24位拆成">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-04-12T13:59:45.767Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Base64算法原理">
<meta name="twitter:description" content="概述Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一，常用于传递HTTP中较长的标识信息、或者对于一些信息进行简单的加密。当我们用记事本打开一些二进制文件时会显示一堆乱码，主要是因为二进制文件中包含了很多无法显示和打印的字符，这种情况下可以对数据进行base64编码，使得记事本等文本工具可以对其处理。 Base64编码原理是将原始数据分组，每组3个字节24位。再将每组的24位拆成">
  
    <link rel="alternate" href="/atom.xml" title="SmileYi" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
</head>
</html>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">SmileYi</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">只要学会破罐子破摔 世界就会豁然开朗</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">首页</a>
        
          <a class="main-nav-link" href="/archives">归档</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://smileyi.gitee.io"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-Base64算法原理" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2019/04/09/Base64算法原理/" class="article-date">
  <time datetime="2019-04-09T02:37:38.000Z" itemprop="datePublished">2019-04-09</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/算法/">算法</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Base64算法原理
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h3><p>Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一，常用于传递HTTP中较长的标识信息、或者对于一些信息进行简单的加密。当我们用记事本打开一些二进制文件时会显示一堆乱码，主要是因为二进制文件中包含了很多无法显示和打印的字符，这种情况下可以对数据进行base64编码，使得记事本等文本工具可以对其处理。</p>
<p>Base64编码原理是将原始数据分组，每组3个字节24位。再将每组的24位拆成4个字节，每个字节6位，字节的取值范围位0~63(0~63所对应的字符分别为:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)，根据数值到处对应的字符，最后拼接成一个字符串。当最后一组不足3个字节时，用“0”补全，每缺一个字节，编码后便在结尾加一个“=”，解码的时候会自动去掉</p>
<h3 id="编码步骤"><a href="#编码步骤" class="headerlink" title="编码步骤"></a>编码步骤</h3><h4 id="1-计算出字符串的二进制值，例如-duxiaoman-d对应的ASCII码100-转为8位二进制是01100100，以此类推duxiaoman对应的二进制值为："><a href="#1-计算出字符串的二进制值，例如-duxiaoman-d对应的ASCII码100-转为8位二进制是01100100，以此类推duxiaoman对应的二进制值为：" class="headerlink" title="1.计算出字符串的二进制值，例如: duxiaoman, d对应的ASCII码100,转为8位二进制是01100100，以此类推duxiaoman对应的二进制值为："></a>1.计算出字符串的二进制值，例如: duxiaoman, d对应的ASCII码100,转为8位二进制是01100100，以此类推duxiaoman对应的二进制值为：</h4><table>
<thead>
<tr>
<th>字母</th>
<th>ascii码</th>
<th>二进制</th>
</tr>
</thead>
<tbody>
<tr>
<td>s</td>
<td>115</td>
<td>01110011</td>
</tr>
<tr>
<td>m</td>
<td>109</td>
<td>01101101</td>
</tr>
<tr>
<td>i</td>
<td>105</td>
<td>01101001</td>
</tr>
<tr>
<td>l</td>
<td>108</td>
<td>01101100</td>
</tr>
<tr>
<td>e</td>
<td>101</td>
<td>01100101</td>
</tr>
<tr>
<td>y</td>
<td>121</td>
<td>01111001</td>
</tr>
<tr>
<td>i</td>
<td>105</td>
<td>01101001</td>
</tr>
</tbody>
</table>
<h4 id="2-将二进制值合并后分组，每6个为一组，并计算出所对应的10进制值，范围在0-63之间-获取对应的字符："><a href="#2-将二进制值合并后分组，每6个为一组，并计算出所对应的10进制值，范围在0-63之间-获取对应的字符：" class="headerlink" title="2.将二进制值合并后分组，每6个为一组，并计算出所对应的10进制值，范围在0~63之间, 获取对应的字符："></a>2.将二进制值合并后分组，每6个为一组，并计算出所对应的10进制值，范围在0~63之间, 获取对应的字符：</h4><p>合并后的二进制串: 011100 110110 110101 101001 011011 000110 010101 111001 011010 01，最后一组01不足6位则用”0”补全，上述字符串共划分为10组，差2组可以整出4，所以在结尾补上2个”=”</p>
<table>
<thead>
<tr>
<th>二进制</th>
<th>十进制</th>
<th>字母</th>
</tr>
</thead>
<tbody>
<tr>
<td>011100</td>
<td>28</td>
<td>c</td>
</tr>
<tr>
<td>110110</td>
<td>54</td>
<td>2</td>
</tr>
<tr>
<td>110101</td>
<td>53</td>
<td>1</td>
</tr>
<tr>
<td>101001</td>
<td>41</td>
<td>p</td>
</tr>
<tr>
<td>011011</td>
<td>27</td>
<td>b</td>
</tr>
<tr>
<td>000110</td>
<td>6</td>
<td>G</td>
</tr>
<tr>
<td>010101</td>
<td>21</td>
<td>V</td>
</tr>
<tr>
<td>111001</td>
<td>57</td>
<td>5</td>
</tr>
<tr>
<td>011010</td>
<td>26</td>
<td>a</td>
</tr>
<tr>
<td>010000</td>
<td>16</td>
<td>Q</td>
</tr>
</tbody>
</table>
<p>将所得字符组合，并在结尾处补上”=”, 得出smileyi的base64编码为：c21pbGV5aQ==</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://smileyi.gitee.io/2019/04/09/Base64算法原理/" data-id="ck5390nqq0000keczn01te7gl" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/原创/">原创</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2019/04/10/Linux基本配置/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Linux基本配置
        
      </div>
    </a>
  
  
    <a href="/2019/04/07/Linux编译安装PHP-Nginx-Mysql/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Linux编译安装PHP+Nginx+Mysql</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Docker/">Docker</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Linux/">Linux</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/MacOS/">MacOS</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Mysql/">Mysql</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Nginx/">Nginx</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/PHP/">PHP</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Swoole/">Swoole</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/算法/">算法</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/网络/">网络</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/原创/">原创</a><span class="tag-list-count">10</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/总结/">总结</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/笔记/">笔记</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/转载/">转载</a><span class="tag-list-count">2</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/01/">一月 2020</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">七月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">六月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">四月 2019</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">三月 2019</a><span class="archive-list-count">4</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/01/05/Swoole之server/">swoole之server</a>
          </li>
        
          <li>
            <a href="/2020/01/02/Certbot-免费的ssl证书/">Certbot--免费的ssl证书</a>
          </li>
        
          <li>
            <a href="/2019/07/13/Nginx限流配置/">Nginx限流配置</a>
          </li>
        
          <li>
            <a href="/2019/06/23/Mysql分区/">Mysql分区</a>
          </li>
        
          <li>
            <a href="/2019/04/15/Rsa算法/">Rsa算法</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 王中艺<br>
      <a href="http://www.beian.miit.gov.cn/">京ICP备18032247号-2</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">首页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



  </div>
</body>
</html>